<!--
 * @Author: spycym
 * @Date: 2022-06-25 17:23:02
 * @LastEditTime: 2022-09-20 19:47:09
 * @LastEditors: spycym
 * @Description: 
 * @FilePath: \epidemic-visualization\src\pages\data\List.vue
 * 不要随便动我的代码哟!
-->
<!-- 数据列表展示+数据上报 -->
<template>
  <div>
    <!-- 顶部查询/数据上报 -->
    <el-form :inline="true" :model="dataForm" class="demo-form-inline">
      <el-cascader
        v-model="dataForm.pca"
        :props="{ label: 'name', value: 'name' }"
        :options="pca"
        size="small"
        style="width: 250px; margin-top: 5px"
        clearable
      ></el-cascader>
      <el-form-item>
        <el-button type="primary" @click="dataSearch" size="small"
          >查询</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="openDialog" size="small"
          >数据上报</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格信息展示 -->
    <el-table
      border
      :data="tableData"
      stripe
      size="small"
      :header-cell-style="{ background: '#185DAD', color: 'white' }"
    >
      <el-table-column
        prop="index"
        label="序号"
        type="index"
        align="center"
        width="50px"
      >
      </el-table-column>
      <el-table-column prop="country" label="国" align="center" width="70px">
      </el-table-column>
      <el-table-column prop="province" label="省" align="center" width="120px">
      </el-table-column>
      <el-table-column prop="city" label="市" align="center" width="120px">
      </el-table-column>
      <el-table-column prop="area" label="区" align="center" width="120px">
      </el-table-column>
      <el-table-column
        prop="confirmed"
        label="新增确诊"
        align="center"
        width="80px"
      >
        <template slot-scope="scope">
          <el-tag type="danger">{{ scope.row.confirmed }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="suspected"
        label="疑似"
        align="center"
        width="80px"
      >
        <template slot-scope="scope">
          <el-tag type="warning">{{ scope.row.suspected }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="dead" label="死亡" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag type="info">{{ scope.row.dead }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="cure" label="治愈" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.cure }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="severe" label="重症" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag type="danger">{{ scope.row.severe }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="outside"
        label="境外输入"
        align="center"
        width="80px"
      >
        <template slot-scope="scope">
          <el-tag>{{ scope.row.outside }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上报时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.inputTime | fmtDate }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150px">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="updateInfo(scope.row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" @click="delInfo(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>
    <!-- 模态框 -->
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="50%"
      v-if="visible"
      :before-close="handleClose"
    >
      <el-form
        :model="uploadForm"
        :rules="dataRules"
        ref="uploadForm"
        label-width="120px"
        class="demo-dataForm"
      >
        <el-form-item label="请选择城市" prop="pca">
          <el-cascader
            v-model="uploadForm.pca"
            :props="{ label: 'name', value: 'name' }"
            :options="pca"
            style="width: 250px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="确诊" prop="confirmed">
          <el-input
            v-model="uploadForm.confirmed"
            style="width: 400px"
          ></el-input>
        </el-form-item>
        <el-form-item label="疑似" prop="suspected">
          <el-input
            v-model="uploadForm.suspected"
            style="width: 400px"
          ></el-input>
        </el-form-item>
        <el-form-item label="死亡" prop="dead">
          <el-input v-model="uploadForm.dead" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="治愈" prop="cure">
          <el-input v-model="uploadForm.cure" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="重症" prop="severe">
          <el-input v-model="uploadForm.severe" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="境外输入" prop="outside">
          <el-input
            v-model="uploadForm.outside"
            style="width: 400px"
          ></el-input>
        </el-form-item>
        <el-button
          type="primary"
          @click="toSubmitData"
          style="width: 150px; margin-left: 215px"
          size="small"
          >提交</el-button
        >
        <el-button @click="visible = false" size="small">取 消</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "@/utils/request";
import pca from "@/assets/pca.json";
export default {
  data() {
    return {
      title: "",
      tableData: [],
      page: 1,
      pageSize: 10,
      total: 30,
      dataForm: {},
      pca,
      uploadForm: {},
      visible: false,
      dataRules: {
        pca: [{ required: true, message: "请选择地区", trigger: "blur" }],
        confirmed: [
          { required: true, message: "请输入确诊病例人数", trigger: "blur" },
        ],
        suspected: [
          { required: true, message: "请输入疑似病例人数", trigger: "blur" },
        ],
        dead: [
          { required: true, message: "请输入死亡病例人数", trigger: "blur" },
        ],
        cure: [
          { required: true, message: "请输入已治愈人数", trigger: "blur" },
        ],
        severe: [
          { required: true, message: "请输入重症病例人数", trigger: "blur" },
        ],
        outside: [
          {
            required: true,
            message: "请输入境外输入病例人数",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    handleClose(done) {
      done();
      this.uploadForm = {};
    },
    // 修改信息
    updateInfo(scope) {
      this.uploadForm = { ...scope };
      this.uploadForm.pca = [
        this.uploadForm.province,
        this.uploadForm.city,
        this.uploadForm.area,
      ];
      this.title = "修改疫情数据信息";
      this.visible = true;
    },
    // 删除信息
    delInfo(scope) {
      //element-ui给Vue.prototype添加的方法
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "/epidemic/deleteById";
        get(url, { id: scope.id }).then((response) => {
          this.$message({ type: "success", message: response.message });
          this.getTableData();
        });
      });
    },
    // 获取表格数据
    async getTableData(province = "", city = "", area = "") {
      let params = {
        page: 1,
        pageSize: 10,
        province,
        city,
        area,
      };
      let res = await get("/epidemic/pageQuery", params);
      this.tableData = res.data.list;
      this.page = res.data.page;
      this.pageSize = res.data.pageSize;
      this.total = res.data.total;
    },
    //数据上报
    openDialog() {
      this.title = "疫情数据上报";
      this.visible = true;
    },
    // 翻页
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    },
    // 查询
    dataSearch() {
      // let province = this.dataForm.pca[0];
      // let city = this.dataForm.pca[1];
      // let area = this.dataForm.pca[2];
      let [province, city, area] = [...this.dataForm.pca];
      this.getTableData(province, city, area);
      this.dataForm.pca = [];
    },
    // 表单提交
    toSubmitData() {
      this.$refs["uploadForm"].validate(async (valid) => {
        if (valid) {
          let [province, city, area] = [...this.uploadForm.pca];
          this.uploadForm = { ...this.uploadForm, province, city, area };
          this.uploadForm.country = "中国";
          let res = await post("/epidemic/saveOrUpdate", this.uploadForm);
          if (res.status == 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            this.visible = false;
            this.getTableData();
            this.uploadForm = {};
          }
        } else {
          this.$notify({
            title: "失败",
            message: "数据上报有误！",
            type: "error",
          });
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
